package c2.group.server.configuration;

import c2.advanced.business.base.ModuleRequirements;
import c2.advanced.server.configuration.AdvServerEnv;
import c2.advanced.server.configuration.ContextListener;
import c2.advanced.server.configuration.SetupContext;
import c2.group.business.GroupBody;
import c2.group.server.GroupServerApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoTemplate;

@Slf4j
@Import(value = {AdvServerEnv.class, ContextListener.class})
@Configuration
public class InitConfig {

    @Bean
    public GroupBody groupBody(AdvServerEnv env, MongoTemplate mongoTemplate) throws Exception {
        log.info("读取到的最终的环境配置：{}", env);
        check(env);

        SetupContext context = new SetupContext();
        context.launcher = GroupServerApplication.launcher;
        context.env = env;
        context.mongoTemplate = mongoTemplate;

        GroupBody body = new GroupBody();
        body.setup(context);
        body.setCode(context.env.getSelf().getCode());

        ModuleRequirements requirements = new ModuleRequirements();
        requirements.config = GroupServerApplication.launcher.conf;

        body.initModules(requirements);

        return body;
    }

    private static void check(AdvServerEnv env) {
        if(env.isDebugShareService()) {
            log.warn("当前处于调试公共服状态！在内网演示环境与外网正式环境都是不允许的！");
            log.warn("仅供本地单独调试公共服时允许开启");
        }
    }

}
